/*
 * @Date: 2019-05-22 21:55:11
 * @LastEditors: SkyBlue
 * @LastEditTime: 2019-06-21 18:10:12
 */
package com.skyblue.controller;

import com.skyblue.entity.User;
import com.skyblue.service.LoginService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LoginController {
    @Autowired
    private LoginService loginService;

    // 接受前端信息
    @RequestMapping(value = "/api/login", method = RequestMethod.POST)
    @ResponseBody
    // mode 1:注册 2：登录
    // msg -1：注册失败 -2：密码错误 1：注册成功 2：登录成功
    // type 0:非管理员 1:管理员
    public Map<String, Integer> goLogin(@RequestBody Map<String, String> map) {
        Map<String, Integer> result = new HashMap<>();
        String account = map.get("account");
        String password = map.get("password");
        if (loginService.check(account).size() > 0) {// 用户表中有
            result.put("mode", 2);
            if (loginService.check(account).get(0).getPassword().equals(password)) {
                result.put("msg", 2);// 密码正确
                if (loginService.check(account).get(0).getIdentifier() == 1) {
                    result.put("type", 1);
                } else {
                    result.put("type", 0);
                }
            } else {
                result.put("msg", -2);
            }
        } else if (loginService.checkApply(account).size() > 0) {// 申请表中有
            result.put("mode", 2);
            if (loginService.checkApply(account).get(0).getPassword().equals(password)) {
                result.put("msg", 2);
                result.put("type", 0);
            } else {
                result.put("msg", -2);
            }
        } else {// 没有该用户，注册
            result.put("mode", 1);
            User newUser = new User();
            newUser.setAccount(account);
            newUser.setPassword(password);
            int s = loginService.registerApply(newUser);
            if (s == 1) {
                result.put("msg", 1);
                result.put("type", 0);
            } else {
                result.put("msg", -1);
            }
        }
        return result;
    }

    @RequestMapping(value = "/api/checking", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> checkLogin(@RequestBody Map<String, String> map) {
        String acc = map.get("account");
        String pass = map.get("password");
        String type = map.get("type");
        Map<String, String> status = new HashMap<>();
        List<User> user = loginService.check(acc);
        status.put("name", user.get(0).getName());
        System.out.println(user.get(0).getName());
        status.put("type", user.get(0).getIdentifier().toString());
        if (user.size() == 1) {
            if (user.get(0).getPassword().equals(pass)) {
                status.put("status", "0");
                return status;
            } else {
                status.put("status", "1");
                return status;
            }
        } else {
            status.put("status", "2");
            return status;
        }
    }

    @RequestMapping(value = "/api/pagecheck", method = RequestMethod.POST)
    @ResponseBody
    public List<User> pageCompare(@RequestBody Map<String, String> account) {
        return loginService.check(account.get("account"));
    }

    @RequestMapping(value = "/api/applyuser", method = RequestMethod.POST)
    @ResponseBody
    public List<List<User>> applyuser(@RequestBody Map<String, String> account) {
        List<List<User>> result = new ArrayList<>();
        result.add(loginService.check(account.get("account")));
        result.add(loginService.checkApply(account.get("account")));
        return result;
    }

    @RequestMapping(value = "/api/handleApply", method = RequestMethod.POST)
    @ResponseBody
    public Object handleApply() {
        return loginService.handleApply();
    }

    @RequestMapping(value = "/api/updataUser", method = RequestMethod.POST)
    @ResponseBody
    public int updataUser(@RequestBody Map<String, String> user) {
        int result;
        User updata = new User();
        updata.setAccount(user.get("account"));
        updata.setCardnum(user.get("cardnum"));
        updata.setPassword(user.get("password"));
        updata.setName(user.get("name"));
        updata.setUserimg(user.get("img"));
        updata.setRealname(user.get("realname"));
        Integer type = Integer.parseInt(user.get("type"));
        if (type == -1) {
            result = loginService.updataApplyUser(updata);
        } else if (type == 0) {
            result = loginService.updataUser(updata);
        } else {
            result = -1;
        }
        return result;
    }

    @RequestMapping(value = "/api/refused", method = RequestMethod.POST)
    @ResponseBody
    public int refused(@RequestBody Map<String, String> id) {
        return loginService.refused(Integer.parseInt(id.get("id")));
    }

    @RequestMapping(value = "/api/approval", method = RequestMethod.POST)
    @ResponseBody
    public int approval(@RequestBody Map<String, String> id) {
        if (loginService.approval(Integer.parseInt(id.get("id"))) > 0) {
            return loginService.refused(Integer.parseInt(id.get("id")));
        } else {
            return -1;
        }
    }

    @RequestMapping(value = "/api/approvalAll", method = RequestMethod.POST)
    @ResponseBody
    public int approvalAll() {
        if (loginService.approvalAll() > 0) {
            return loginService.refusedAll();
        } else {
            return -1;
        }
    }

    @RequestMapping(value = "/api/refusedAll", method = RequestMethod.POST)
    @ResponseBody
    public int refusedAll() {
        return loginService.refusedAll();
    }

    @RequestMapping(value = "/api/checkUser", method = RequestMethod.POST)
    @ResponseBody
    public List<User> checkuser() {
        return loginService.checkUser();
    }
}
